
import java.awt.Button;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Label;
import java.awt.TextField;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JPasswordField;

import users.Prof;
import client.AESSClient;
import client.OfficerClient;

import common.ChatIF;


/**
 * This class constructs the GUI for a chat client.  It implements the
 * chat interface in order to activate the display() method.
 * 
 * 2012.11.04 Yunja Choi
 * Created for an in-class example
 * *
 */
public class ClientGUI extends JFrame implements ChatIF 
{
  //Class variables *************************************************
  
  /**
   * The default port to connect on.
   */
  final public static int DEFAULT_PORT = 5555;
  
  //Instance variables **********************************************
  
  /**
   * The instance of the client that created this ConsoleChat.
   */
  AESSClient client;

  // declaration of basic GUI components
// private Button logOffB = new Button("LogOff");
 private Button logInB = new Button("LogIn");
// private Button sendB = new Button("Send");
 private Button quitB = new Button("Quit");
 private JPasswordField PSTxF = new JPasswordField("");
 private TextField IDTxF = new TextField("");
// private TextField message = new TextField();
 private Label PSLB  = new Label("Password: ", Label.RIGHT);
 private Label IDLB = new Label("ID: ", Label.RIGHT);
// private Label messageLB = new Label("Message: ", Label.RIGHT);
 //private List messageList = new List();
  
 
  //Constructors ****************************************************

  /**
   * Constructs an instance of the ClientConsole UI.  Login removed
   * in phase 3 as the server will prompt for it.
   *
   * @param host The host to connect to.
   * @param port The port to connect on.
   */
  public ClientGUI(String host, int port, String id) 
  {
	  super("AESS System ****** by.노인공경");
	  setLayout(null);
	  setSize(500,300);
	  setVisible(true);
	  setBackground(Color.white);
	  
	  try 
	    {
	      client= new AESSClient(host, port, this);
	      this.setResizable(false);
	      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    } 
	    catch(IOException exception) 
	    {
	      System.out.println("Error: Can't setup connection!"
	                + " Terminating client.");
	      System.exit(1);
	    }
	 
	 // create a GUI window 
	 makeWindow(host, port);      
  } 
  //Instance methods ************************************************
  
	private void makeWindow(final String host, final int port){
		  
		//   portTxF.setText(String.valueOf(port));
		//   hostTxF.setText(host);	  
		  logInB.addActionListener(new ActionListener()
		  {
			  public void actionPerformed(ActionEvent e){
				  client.handleMessageFromClientUI("#login"+":"+IDTxF.getText()+"="+PSTxF.getText());
				  
				 while(!client.isLogin){
					  System.out.println("wait response");
					 
				  }
			
				  String permission = client.getPermission();
				  
				  if(permission.equals("2")){
					  // User : professor
					  makeProfessor(host, port);
				  } else if(permission.equals("1")){
					  // User : Admin
					  makeOfficer(host, port);
				  } else if(permission.equals("3")){
					  // User : TA
					  makeTA(host, port);
				  } else if(permission.equals("4")){
					  // User : Student
					  makeStudent(host, port);
				  }
				  
				  setVisible(false);	
			  }
			  
		  });
		  	  
		  quitB.addActionListener(new ActionListener()
		  {
			  public void actionPerformed(ActionEvent e){
				  client.handleMessageFromClientUI("#quit");
				  System.exit(-1);
			  }
		  });
		  
			
		  JPanel bottom = new JPanel(){
			  Image Back = Toolkit.getDefaultToolkit().getImage("src/mainbg.jpg");
			  public void paintComponent(Graphics g){
				     //super.paintComponents(g);
				     g.drawImage(Back, 0, 0, 500, 280, this);
				    }
		  };
		  
		  bottom.setLayout(null);
		  bottom.setSize(500,300);
		  bottom.setLocation(0, 0);
			
		  bottom.setVisible(true);
		
		  bottom.add(PSLB);
		  bottom.add(PSTxF);
		  bottom.add(IDLB);
		  bottom.add(IDTxF);
		 bottom.add(logInB);
		 bottom.add(quitB);
		 IDLB.setBounds(280, 100, 70 , 20);
		 IDTxF.setBounds(350, 100, 130 , 20);
		 PSLB.setBounds(280, 130, 70 , 20);
		 PSTxF.setBounds(350, 130, 130 , 20);
		 logInB.setBounds(350, 180, 50 , 30);
		 quitB.setBounds(420, 180, 50 , 30);
		 
		 add(bottom); 	
	}

	private void makeProfessor(String host, int port)
	{
		
		  ProfessorGUI Professor = new ProfessorGUI(host, port, this);
	}
	
	
	private void makeTA(String host, int port)
	{
		
		  TAGUI TA = new TAGUI(host, port, this);
	}
	
	private void makeStudent(String host, int port)
	{
		
		  StudentGUI Student = new StudentGUI(host, port, this);
	}

	public void accept() 
	{
	  try
	  {
	    BufferedReader fromConsole = 
	      new BufferedReader(new InputStreamReader(System.in));
	    String message;
	
	    while (true) 
	    {
	      message = fromConsole.readLine();
	      client.handleMessageFromClientUI(message);
	    }
	  } 
	  catch (Exception ex) 
	  {
	    System.out.println
	      ("Unexpected error while reading from console!");
	  }
	}

  /**
   * This method overrides the method in the ChatIF interface.  It
   * displays a message onto the screen.
   *
   * @param message The string to be displayed.
   */
  public void display(String message) 
  {
 //   messageList.add(message);
  //  messageList.makeVisible(messageList.getItemCount()-1);
  }

  public void makeOfficer(String host, int port)
  {
	  OfficeGUI Officer = new OfficeGUI(host, port, this);
  }
  
  //Class methods ***************************************************
  
  /**
   * This method is responsible for the creation of the Client UI. 
   * Login removed in phase 3 as the server will prompt the user 
   * for it. 
   *
   * @param args[0] The host name to connect to.
   * @param args[1] The port to connect on.  Added in phase 2.
   */
  public static void main(String[] args) 
  {
    String host = "";
    int port = 0;
    String loginID = null;
   
/*        
    try
    {
      loginID = args[0]; 
    }
    catch(ArrayIndexOutOfBoundsException e)
    {
    	System.out.println("ERROR - No login ID specified. Connection aborted.");
    	System.exit(1);
    }
  */  
    try
    {
      host = args[1];
    }
    catch(ArrayIndexOutOfBoundsException e)
    {
      host = "localhost";
    }
    
    try
    {
      port = Integer.parseInt(args[2]);
    }
    catch(Throwable t)
    {
      port = DEFAULT_PORT;
    }
    
    try{
       // create ClientGUI instead of clientConsole
       ClientGUI chat = new ClientGUI(host, port, loginID);
       chat.accept();  //Wait for console data
       
    }catch(Exception e){
    	e.printStackTrace();
    }    
  }
  
  
}
//End of ClientGUI class